Imports Microsoft.VisualBasic
Imports System.Xml
Imports System.Xml.XmlNode
Imports System.Xml.Serialization

<Serializable()> _
Public Class InjurySetup
    Inherits BaseSetup



    Private _recoveryWeeks As Integer  'hamstring usually 20days

    'Private _injuryAgeMap As Hashtable

    Private _recoveryAgeMap As Hashtable

    'chances of getting a certain injury (Hamstring) at a certain age.
    ' <16 -----         20% chance
    ' 16<=age<20        20% chance
    ' 21<=age<25        10% chance
    ' 25<=age<30        10% chance
    ' 30<=age<35        10% chance
    ' 35<=age<40        10% chance
    ' >=40              20% chance
    '                   ---



    'if you get injured, you have a 20 % chance of it being Hamstring if under 16 yrs of age, and 10% Concussion
    ' strength , agility=200
    ' sum=100
    ' 50% chance of getting injuried.(50% risk)

    'recovery = 10days
    'actual recovery is based on player stats * recovery period.
    ' e.g recovery is based on strength, agility (200)
    ' good example .the sum = 150.
    ' 150/200 = 3/4 * recovery
    '         = 4/3 * 10 = 13.33
    ' so the player will take 13days
    '
    '
    ' bad example = 50
    ' 50/200 = 1/4
    '          4/1 * 10 = 40days  
    ' weak player will take 40days


    '               AgeRange1 AgeRange2 AgeRange3 AgeRange4 AgeRange5 AgeRange6 AgeRange7
    'Injury1           20        20        20        20        20        20        20
    'Injury2           20        20        20        20        20        20        20
    'injury3           60        20        20        20        20        20        20
    'injuryn           ..        ..        ..        ..        ..        ..        ..
    '------------------------------------------------------------------------------------
    '                 100       100       100       100       100       100       100
    '------------------------------------------------------------------------------------

    'if you are 50% of injury
    ' 1. you dont get injured.
    '
    '
    '
    ' 2. you get injured.
    '   if (ageRange1)
    '     you are 60% ,20 %, 205`
    '
    '
    '
    '
    '

    Public Sub New()
        '_injuryAgeMap = New Hashtable
        'For Each key As String In Cons.GET_AGERANGES
        '    _injuryAgeMap.Add(key, "-1")
        'Next

        _recoveryAgeMap = New Hashtable
        For Each key As String In Cons.GET_AGERANGES
            _recoveryAgeMap.Add(key, "-1")
        Next
    End Sub


    Public Property RecoveryWeeks() As Integer
        Get
            Return _recoveryWeeks
        End Get
        Set(ByVal value As Integer)
            _recoveryWeeks = value
        End Set
    End Property

    'Public Property InjuryAgeMap() As Hashtable
    '    Get
    '        Return _injuryAgeMap
    '    End Get
    '    Set(ByVal value As Hashtable)
    '        _injuryAgeMap = value
    '    End Set
    'End Property

    Public Property RecoveryAgeMap() As Hashtable
        Get
            Return _recoveryAgeMap
        End Get
        Set(ByVal value As Hashtable)
            _recoveryAgeMap = value
        End Set
    End Property


    Public Overrides Function SetupToXML() As String
        Dim xml As String = ""

        xml &= "<Setup>"
        xml &= "<recoveryWeeks> " & Me._recoveryWeeks & "</recoveryWeeks>"

        'xml &= "<InjuryAgeRanges>"
        'For Each key As Object In _injuryAgeMap.Keys
        '    xml &= "<" & key & ">" & _injuryAgeMap(key) & "</" & key & ">"
        'Next
        'xml &= "</InjuryAgeRanges>"


        xml &= "<RecoveryAgeRanges>"
        For Each key As Object In _recoveryAgeMap.Keys
            xml &= "<" & key & ">" & _recoveryAgeMap(key) & "</" & key & ">"
        Next
        xml &= "</RecoveryAgeRanges>"


        xml &= "</Setup>"

        Return xml

    End Function

    Public Overrides Function XMLToSetup(ByVal xml As String) As BaseSetup

        Dim doc As New XmlDocument

        If (xml.Length > 0 And Not xml.Equals("<Setup />")) Then
            doc.LoadXml(xml)

            '_trainingMap = New Hashtable
            Dim value As String
            value = doc.GetElementsByTagName("recoveryWeeks").ItemOf(0).InnerText
            _recoveryWeeks = value

            'Dim injuryAgeRangeNode As XmlNode = doc.GetElementsByTagName("InjuryAgeRanges").ItemOf(0)

            'For Each key As String In Cons.GET_AGERANGES
            '    value = injuryAgeRangeNode.SelectNodes(key).ItemOf(0).InnerText
            '    _injuryAgeMap.Item(key) = value
            'Next

            Dim recoveryAgeRangeNode As XmlNode = doc.GetElementsByTagName("RecoveryAgeRanges").ItemOf(0)

            For Each key As String In Cons.GET_AGERANGES
                value = recoveryAgeRangeNode.SelectNodes(key).ItemOf(0).InnerText
                _recoveryAgeMap.Item(key) = value
            Next
        End If
        Return Me
    End Function

    Public Overrides Function Clone() As BaseSetup

        Dim _clone As New InjurySetup
        _clone.RecoveryWeeks = Me.RecoveryWeeks



        Dim deepCopy1 As New Hashtable
        For Each ageRange As String In Cons.GET_AGERANGES
            'deepCopy1.Add(ageRange, Me.InjuryAgeMap.Item(ageRange))

        Next
        '_clone.InjuryAgeMap = deepCopy1



        Dim deepCopy2 As New Hashtable
        For Each ageRange As String In Cons.GET_AGERANGES
            deepCopy2.Add(ageRange, Me.RecoveryAgeMap.Item(ageRange))

        Next
        _clone.RecoveryAgeMap = deepCopy2


        Return _clone
    End Function

    Public Overrides Function ToString() As String
        Return ""
    End Function

    Public Overrides Function ValidateBaseSetup() As Collection
        Dim errList As New Collection
        'check that all age ranges are integers

        Dim errRow As ErrorRow
        Dim val As Integer
        Dim injuryTotal As Integer
        Dim recoveryTotal As Integer

        'Try
        '    val = CInt(_injuryAgeMap.Item(Cons.AGERANGE1))
        '    injuryTotal += val
        'Catch ex As Exception
        '    errRow = New ErrorRow
        '    errRow.Field = "Injury Age Range 1"
        '    errRow.Value = _injuryAgeMap.Item(Cons.AGERANGE1)
        '    errRow.Message = Cons.VALIDATION_ERROR_INVALID_AgeRangeValue
        '    errList.Add(errRow)
        'End Try

        'Try
        '    val = CInt(_injuryAgeMap.Item(Cons.AGERANGE2))
        '    injuryTotal += val
        'Catch ex As Exception
        '    errRow = New ErrorRow
        '    errRow.Field = "Injury Age Range 2"
        '    errRow.Value = _injuryAgeMap.Item(Cons.AGERANGE2)
        '    errRow.Message = Cons.VALIDATION_ERROR_INVALID_AgeRangeValue

        '    errList.Add(errRow)
        'End Try

        'Try
        '    val = CInt(_injuryAgeMap.Item(Cons.AGERANGE3))
        '    injuryTotal += val
        'Catch ex As Exception
        '    errRow = New ErrorRow
        '    errRow.Field = "Injury Age Range 3"
        '    errRow.Value = _injuryAgeMap.Item(Cons.AGERANGE3)
        '    errRow.Message = Cons.VALIDATION_ERROR_INVALID_AgeRangeValue
        '    errList.Add(errRow)

        'End Try

        'Try
        '    val = CInt(_injuryAgeMap.Item(Cons.AGERANGE4))
        '    injuryTotal += val
        'Catch ex As Exception
        '    errRow = New ErrorRow
        '    errRow.Field = "Injury Age Range 4"
        '    errRow.Value = _injuryAgeMap.Item(Cons.AGERANGE4)
        '    errRow.Message = Cons.VALIDATION_ERROR_INVALID_AgeRangeValue
        '    errList.Add(errRow)

        'End Try

        'Try
        '    val = CInt(_injuryAgeMap.Item(Cons.AGERANGE5))
        '    injuryTotal += val
        'Catch ex As Exception
        '    errRow = New ErrorRow
        '    errRow.Field = "Injury Age Range 5"
        '    errRow.Value = _injuryAgeMap.Item(Cons.AGERANGE5)
        '    errRow.Message = Cons.VALIDATION_ERROR_INVALID_AgeRangeValue
        '    errList.Add(errRow)

        'End Try

        'Try
        '    val = CInt(_injuryAgeMap.Item(Cons.AGERANGE6))
        '    injuryTotal += val
        'Catch ex As Exception
        '    errRow = New ErrorRow
        '    errRow.Field = "Injury Age Range 6"
        '    errRow.Value = _injuryAgeMap.Item(Cons.AGERANGE6)
        '    errRow.Message = Cons.VALIDATION_ERROR_INVALID_AgeRangeValue
        '    errList.Add(errRow)

        'End Try

        'Try
        '    val = CInt(_injuryAgeMap.Item(Cons.AGERANGE7))
        '    injuryTotal += val
        'Catch ex As Exception
        '    errRow = New ErrorRow
        '    errRow.Field = "Injury Age Range 7"
        '    errRow.Value = _injuryAgeMap.Item(Cons.AGERANGE7)
        '    errRow.Message = Cons.VALIDATION_ERROR_INVALID_AgeRangeValue
        '    errList.Add(errRow)
        'End Try

        ''check that age ranges sum to 100%
        'If (injuryTotal <> 100) Then
        '    errRow = New ErrorRow
        '    errRow.Field = "Injury Age Range Total"
        '    errRow.Value = injuryTotal
        '    errRow.Message = Cons.VALIDATION_ERROR_INVALID_AgeRangeSum
        '    errList.Add(errRow)
        'End If


        ''recovery weeks cannot be less than 0
        'If _recoveryWeeks < 0 Then
        '    errRow = New ErrorRow
        '    errRow.Field = "Recovery Weeks"
        '    errRow.Value = _recoveryWeeks
        '    errRow.Message = Cons.VALIDATION_ERROR_INVALID_RecoveryWeeksValue
        '    errList.Add(errRow)
        'End If


        'If errList.Count > 0 Then
        '    Return errList
        'Else
        '    Return Nothing
        'End If
    End Function

    Public Overrides Function DefaultXML() As String
        Return "<Setup />"
    End Function
End Class
